

***** This program extracts data for the 61st Round, Schedule 1 into Stata format. ********

/* The output is finally two files
	1) 61_round_Sch1.dta which is stored in the directory $output. This has data at household itemcode level along with pce, pce_365, and hhsize. 
	2) 61st_rnd_HhChar_data.dta which has the other household characteristics and is stored in the directory "$raw_data\61st_round\1"

	NOTE: THE DECIMAL PLACES FOR THE DIFFERENT VARIABLES ARE NOT BEING PUT IN AT THIS STAGE. WILL NEED TO DO THIS AT SOME OTHER POINT I THINK...
	
	NOTE: LEVEL 5 DOES NOT SEEM TO HAVE MULTIPLIER DATA. NOT SURE WHY!!
	*/


/* The raw data is stored in files RMAH`i'C01.txt (i going from 1 to 8) with each file having data for different states.
   Each line in the datasets refers to data for a particular block. The varaiable 'level' tells us which block the line corresponds to.
   This do file extracts data for different levels using dictionary files level`i'.dct
	*/

set more off
clear all
// set mem 2500m

It seems 
block 11 = level 9
block 5 = level 5
block 6 = level 5
block 3b = level 3
block 3a = level 2
b 4 = l4




cd "P:\Sid\Anil_replication\Final_for_publication\Data\Stata_data_raw\NSSO_61_stata\"

use "Block 5_Monthly consumption of food, pan, tobacco and intoxicants.dta"
rename B5_q1 itemCode 
sum itemCode
// cd "$raw_data\61st_round\1"
// log using 61st_rnd_Sch1_extract.txt, replace
// foreach j in 1 2 3 4 5 6 7 8 9 {
// 	forvalues i=1/8 {

		infile using level`j'.dct if level==`j', using(RMAH`i'C01.txt) clear


		* Generating household identifier *
// 		gen double hhid=fsu*10000+hamlet_group*1000+ snd_stg_stra*100+hh_no
// 		cap su itemCode
// 		if _rc==0 {
// 		 duplicates report hhid itemCode
// 		 tab itemCode
// 		}

		* Generate combined multiplier which can be used for the entire sample *
// 		gen mult_comb=mult/100 if nss==nsc
// 		replace mult_comb=mult/200 if nss!=nsc
		gen mult_comb=MLT/100 if NSS==NSC
		replace mult_comb=MLT/200 if NSS!=NSC
		
// 		drop filler1 filler2

		compress
		save "$raw_data\61st_round\1\level`j'_`i'.dta", replace

		if `i'==1 {

			save "$raw_data\61st_round\1\61st_rnd_level`j'.dta", replace

			}
		else {

			append using "$raw_data\61st_round\1\61st_rnd_level`j'.dta"
			save "$raw_data\61st_round\1\61st_rnd_level`j'.dta", replace
			}
			


	}
}



foreach j in 1 2 3 4 5 6 7 8 9 {
	forvalues i=1/8 {

	erase "$raw_data\61st_round\1\level`j'_`i'.dta"
	
	
	}
}
	
	

/* Appending all datasets which have consumption data at household itemcode level. This should be a massive dataset eventually */
use "$raw_data\61st_round\1\61st_rnd_level5.dta", clear
foreach j in 6 7 8 9 {
	append using "$raw_data\61st_round\1\61st_rnd_level`j'.dta"
}
sort hhid itemCode
duplicates report hhid itemCode
su itemCode
save "$raw_data\61st_round\1\61st_rnd_C_data.dta", replace


/* Merging datasets which have household and person information on characteristics */
use "$raw_data\61st_round\1\61st_rnd_level1.dta", clear
sort hhid
save "$raw_data\61st_round\1\61st_rnd_level1.dta", replace

use "$raw_data\61st_round\1\61st_rnd_level2.dta", clear
sort hhid
save "$raw_data\61st_round\1\61st_rnd_level2.dta", replace

use "$raw_data\61st_round\1\61st_rnd_level3.dta", clear
sort hhid
save "$raw_data\61st_round\1\61st_rnd_level3.dta", replace

use "$raw_data\61st_round\1\61st_rnd_level4.dta", clear
sort hhid srl_n_m
foreach j in 1 2 3 {
	merge m:1 hhid using "$raw_data\61st_round\1\61st_rnd_level`j'.dta"
	drop _merge
}
save "$raw_data\61st_round\1\61st_rnd_HhChar_data.dta", replace

/* Merging essential household characteristics data with the consumption data */
keep hhid srl_n_m pce pce_365 hhsize sector state_region mult_comb
keep if srl_n_m==1
sort hhid
merge 1:m hhid using "$raw_data\61st_round\1\61st_rnd_C_data.dta"
save "$output\61_round_Sch1.dta", replace

foreach j in 1 2 3 4 5 6 7 8 9 {
	erase "$raw_data\61st_round\1\61st_rnd_level`j'.dta"
}
* erase "$raw_data\61st_round\1\61st_rnd_C_data.dta"

// log close
